package com.template.controller;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.CircleCaptcha;
import cn.hutool.captcha.LineCaptcha;
import cn.hutool.captcha.ShearCaptcha;
import com.template.dto.User;
import com.template.result.PageBean;
import com.template.result.Result;
import com.template.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Date;
import java.util.List;

@Slf4j
@RestController
//@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private RedisTemplate redisTemplate;


    //登录
    @PostMapping("/login")
    public Result login(@RequestBody User user, HttpServletRequest request){
        String captcha=user.getCaptcha();
        String YEScode=(String) request.getSession().getAttribute("verifyCode");
        System.out.println(YEScode);
        if(!captcha.equalsIgnoreCase(YEScode)){
            return Result.error("验证码错误");
        }
        System.out.println(YEScode);
        Result login = userService.login(user);
        return login;
    }

    //生成验证码
    @GetMapping("/getCaptcha")
    public Result getVerify(HttpServletRequest request) {
        //定义图形验证码的长、宽、验证码字符数、干扰线宽度
//        LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(150, 40);
//        CircleCaptcha lineCaptcha = CaptchaUtil.createCircleCaptcha(150, 40);
        ShearCaptcha lineCaptcha = CaptchaUtil.createShearCaptcha(150, 40);
        //获取验证码中的文字内容
        request.getSession().setAttribute("verifyCode", lineCaptcha.getCode());
        System.out.println(lineCaptcha.getCode());
        String base64String = "";
        try {
            base64String = "data:image/png;base64," + lineCaptcha.getImageBase64();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.success(base64String);
    }

    // 注册
    @PostMapping("/regist")
    public Result regist(@RequestBody User user){
        log.info("注册的用户信息{}",user);
        Result regist=userService.add(user);
        return regist;
    }

//    新增
    @PostMapping("/user/add")
    public Result add(@RequestBody User user){
        log.info("新增:"+user);
        Result add=userService.add(user);
        return add;
    }

    //分页获取用户列表
    @GetMapping("/user")
    public Result getList(@RequestParam(defaultValue = "1")Integer page,
                          @RequestParam(defaultValue = "10")Integer pageSize){
        log.info("分页查询，参数：{}，{}",page,pageSize);

        //构造redis中的key
//        String key="user_all";
//
//        //查询redis中是否存在用户数据
//        List<User> list =(List<User>) redisTemplate.opsForValue().get(key);
//        if(list !=null &&list.size()>0){
//            //如果存在，直接返回，无需查询数据库
//            return Result.success(list);
//        }

        PageBean pageBean=userService.getList(page,pageSize);
        return Result.success(pageBean);
    }

//    删除用户
    @DeleteMapping("/user/del")
    public Result delById(@RequestParam Long id){
        log.info("删除用户{}",id);
        userService.delById(id);
        return Result.success();
    }

//    获取所编辑的用户信息
    @GetMapping("/user/getEdit")
    public Result getEditById(@RequestParam Long id){
        log.info("所要编辑的用户{}回显信息",id);
        User user=userService.getEditById(id);
        return Result.success(user);
    }

    //提交编辑的信息
    @PutMapping("/user/edit")
    public Result editById(@RequestBody User user){
        log.info("修改的用户信息{}",user);
        Result edit=userService.editUserById(user);
        return edit;
    }

//    将查到的数据导出成excel





}
